<template>
	<view class="uni-padding-wrap">
		<view style="width: 100%;height: 150px;background: #195B40;text-align: center;color: white;font-weight: 600;">
			<view style="padding: 20px auto;line-height: 80px;">
				企业名称:{{companyname}}
				</view>
		</view>
		<form @submit="formSubmit" @reset="formReset"  style="height: auto;width: auto;">
			<view style="background: white;border-radius: 24px 24px 0px 0px ;padding: 5px 20px;margin:-50px 3% 0px 3%">
			
			<view class="uni-form-item uni-column">
				<input class="uni-input" name="company" style="display: none;" disabled="true" type="number" v-model="personInfo.company" placeholder="" ></input>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">姓名</view>
				<input class="uni-input" v-model='personInfo.name' name="name" placeholder="请输入姓名" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">证件号码</view>
				<input class="uni-input" name="cardId" type="text" v-model="personInfo.cardId" placeholder="请输入证件号码" />
			</view>
			
			<view class="uni-form-item uni-column">
				<view class="title">联系方式</view>
				<view style="height: 40px;">
				<input style="width: 50%;float: left;" class="uni-input" type="number" name="tel" v-model='personInfo.tel' placeholder="请输入联系电话" />
				<button style="float: left;font-size: 14px;width: 40%;height: 40px;"  open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机</button>
				</view>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">当前居住地</view>
				<input class="uni-input" v-model="personInfo.nowAddress" name="nowAddress" placeholder="请输入当前居住地" />
			</view>
			 <view class="uni-form-item uni-column">
				<view class="title">是否为湖北户籍</view>
				<radio-group name="isHubei">
					<label>
						<radio value="1" /><text>是</text>
					</label>
					<label>
						<radio value="0" /><text>否</text>
					</label>
				</radio-group>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">是否为本地户口</view>
				<radio-group name="native_str">
					<label>
						<radio value="1" /><text>是</text>
					</label>
					<label>
						<radio value="0" /><text>否</text>
					</label>
				</radio-group>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">是否发热</view>
				<radio-group name="isTouch">
					<label>
						<radio value="1" /><text>是</text>
					</label>
					<label>
						<radio value="0" /><text>否</text>
					</label>
				</radio-group>
			</view>

			<view class="uni-btn-v">
				<button form-type="submit" :disabled="empowerDisabled" type='primary' style="background:#195B40">提交</button>
				<button type="default" form-type="reset">重置</button>
			</view>
			</view>
		</form>
	</view>
</template>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}
	.headerTitle{
		font-size: 18px;
		text-align: center;
	}
	.extraInfo {
		font-size: 24rpx;
		color:#666666;
		margin-top: 0px;
	}
	.headerQuestion{
		font-size: 18px;
	}
	.headerDesc{
		color:#7A7E83;
		font-size: 16px;
	}
	.headerRedDesc{
		color:red;
		font-size: 16px;
		display: inline;
	}

	.manageIn {
		position: absolute;
		top: 10rpx;
		right: 30rpx;
		color: #007aff;
	}
	.uni-padding-wrap{
		width: 100%;
		padding: unset;
	}
	.uni-input{
		border: 1px solid #DDDDDD;
	}
</style>
<script>
	var graceChecker = require('common/graceChecker.js');
	import moment from 'moment';
	import {getWxBindMobile} from '../../api/index.js'
	import {addPerson} from '../../api/index.js';
	import {findCompany} from '../../api/index.js'
	import {
		mapState
	} from 'vuex';
	export default {
		computed: mapState(['hasLogin']),
		data() {
			return {
				ifshow:false,
				'today':moment().format('YYYY/MM/DD'),
				company:'',
				companyDisabled: false,
				CertificatesNum:'',
				disabled:false,
				companyname:'',
				empowerDisabled:true,
				empowerDisabled1:true,
				personInfo:{
					name:'',
					cardId:'',
					tel:'',
					company:'',
					nowAddress:'',
				},
			}
		},
		onLoad: function(option) {
			var that =this
			var urldata =decodeURIComponent(option.q)
			option.id = urldata.slice(30)
			var company_id =decodeURIComponent(option.id)
			that.personInfo.company =company_id
			if(option.id) {
				console.log(option.id)
				var _id ={
					company_id:company_id
				}
				wx.setStorageSync("companyId",company_id)
				var _data=findCompany(_id)
				// Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "xx"}
				Promise.resolve(_data).then(function (result) { 
					if(result.code=='0'){
						that.companyname = result.data.CompanyName
					}
					 })
				this.personInfo.company = decodeURIComponent(option.id);
				this.companyDisabled = true;
			}
		},
		methods: {
			getPhoneNumber(e) {
				  	 var datelist={
				  		 'ivStr':e.detail.iv,
				  		 'session_key': wx.getStorageSync("session_key"),
				  		 'encryptedData':e.detail.encryptedData,
				  		'code':wx.getStorageSync("code"),
				  	 }
					 console.log(datelist)
					 var retData1=getWxBindMobile(datelist)
					 console.log(retData1)
					 var that =this
					 // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "xx"}
					 Promise.resolve(retData1).then(function (result) { 
					 	if(result.code=='0'){
							that.empowerDisabled1 =  true
							that.personInfo.tel =result.data.phoneNumber
							// 将电话号码存储
							wx.setStorageSync("tel", result.data.phoneNumber);
					 	}
					 	 })
				  	 console.log(datelist)
			    },
			formSubmit: function(e) {
				//定义表单规则
				var rule = [
					
					{
						name: "name",
						checkType: "string",
						checkRule: "1,4",
						errorMsg: "姓名应为1-4个字符"
					},
					{
						name: "company",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "请扫码进入"
					},
					{
						name: "cardId",
						checkType: "notnull",
						checkRule: "15,18",
						errorMsg: "请填写正确的身份证信息"
						},
						{
							name: "tel",
							checkType: "notnull",
							checkRule: "11",
							errorMsg: "请填写电话号码"
						},
						{
							name: "nowAddress",
							checkType: "notnull",
							checkRule: "",
							errorMsg: "请填写居住地"
						},
					
				];
				// 进行表单检查
				
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if (!checkRes) {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				} else {
					 var retData2=addPerson(e.detail.value)
					 Promise.resolve(retData2).then(function (result) { 
					 	if(result.code=='0'){
					 		console.log(result)
					 		console.log(result.data.SysId,'111')
							wx.setStorageSync("person_name", result.data.Name);
							wx.setStorageSync("personid", result.data.SysId);
							uni.navigateTo({
							    url: './temperature?id='+result.data.SysId
							});
					 	}
					 	 })
				}
			},
			formReset: function(e) {
				console.log('清空数据')
			}

		},
		onShow: function() {
			if(wx.getStorageSync("personid") != ''){
					uni.navigateTo({
						url: './temperature?id='+wx.getStorageSync("personid")
					});
			}
			var that = this;
			console.log(this.empowerDisabled)
			  // 查看是否授权
			  wx.getSetting({
			   success: function (res) {
			    if (res.authSetting['scope.userInfo']) {
					that.empowerDisabled =false
					if(that.empowerDisabled1 != ''){
						
					}else{
						that.empowerDisabled1 =false
					}
					
						var _id ={
							company_id:wx.getStorageSync("companyId")
						}
					var _data=findCompany(_id)
					console.log(_data)
					// Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "xx"}
					Promise.resolve(_data).then(function (result) { 
						if(result.code=='0'){
							that.companyname = result.data.CompanyName
						}
						 })
			    }else{
			}
			   },
			
			  }),
			this.companyDisabled = true;
			if(wx.getStorageSync("companyYN")!=''){
				that.companyYN=wx.getStorageSync("companyYN")
			}
		},
		mounted() {
			this.personInfo.tel =wx.getStorageSync("tel")
			if(wx.getStorageSync("personid") != ''){
					uni.navigateTo({
						url: './temperature?id='+wx.getStorageSync("personid")
					});
			}
		}
	}
</script>
